
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
const db = cloud.database()
const _ = db.command

/**
 * 角色相关公共方法
 */

/**
 * 根据用户ID查询角色相关信息
 * @param userId
 */
export async function getRolesByUserId(userId:string) {
  console.log(`入参UserId=${userId}`)
  //2.1 查询已经拥有的ID
  let adminRoles = await nw.db.findListByWhereJson({
    dbName:"adminRole",
    whereJson:{
      adminId:_.eq(userId),
      deleted:_.eq(0)
    }
  })
  console.log("查询到的adminRole")
  console.log(adminRoles)

  if (nw.util.isNull(adminRoles)||adminRoles.length===0){
    return new Promise<any[]>((resolve, _)=>resolve([]))
  }

  let roleIds = adminRoles.map(item=>item.roleId)
  console.log(`roleIds=${JSON.stringify(roleIds)}`)
  let roles = await nw.db.findListByWhereJson({
    dbName:"role",
    whereJson:{deleted:_.eq(0),_id:_.in(roleIds)}
  })
  console.log(`roles=${JSON.stringify(roles)}`)
  return new Promise<any[]>((resolve, _)=>resolve(roles))

}
